package com.wpw.web;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @ClassName: JsCreate
 * @CreateDate: 2020-03-13 11:29
 * @author: wupengwei
 */
public class JsCreate
{


    /*var codes = [];
    for (var i = 0; i <= 200; i++) {
        $("#plr_code").val(codes[i]);
        $parts_list_return.addTr();
    }

    for (var i = 0; i <= 200; i++) {$("#plr_code").val(codes[i]);$parts_list_return.addTr();}

    */


    public static void main(String[] args) {
        String filePath = "/Users/wupengwei/Desktop/codes.txt";
        Path path = Paths.get(filePath);
        int pageSize = 200;//每次处理多少个
        try {
            List<String> codes = Files.readAllLines(path, StandardCharsets.UTF_8);
            int totalSize = codes.size();
            System.out.println("共" + totalSize + "条数据");

            List<List<String>> dataList = new ArrayList<>();
            List<String> data = null;
            for (int i = 0; i < codes.size(); i++) {
                String code = codes.get(i);

                if (i % pageSize == 0) {
                    data = new ArrayList<>();
                    dataList.add(data);
                }
                data.add(code);

            }

            List<StringBuilder> sbList = new ArrayList<>();
            for (List<String> list : dataList) {
                StringBuilder sb = new StringBuilder("var codes = [");

                for (int i = 0; i < list.size(); i++) {
                    String code = list.get(i);
                    if (i == list.size() - 1) {
                        sb.append("'"+ code +"'");
                    } else {
                        sb.append("'"+ code +"',");
                    }
                }
                sb.append("];");
                sbList.add(sb);
            }

            String jsFile = "/Users/wupengwei/Desktop/codes.js";
            Path jsPath = Paths.get(jsFile);
            BufferedWriter writer = Files.newBufferedWriter(jsPath, StandardCharsets.UTF_8);
            for (StringBuilder codeArrayStr : sbList) {
                try {
                    writer.write("/******************************/");
                    writer.newLine();
                    writer.write(codeArrayStr.toString());
                    writer.newLine();

                    String code = "for (var i = 0; i < "+pageSize+"; i++) {$(\"#plr_code\").val(codes[i]);$parts_list_return.addTr();}";
                    writer.write(code);
                    writer.newLine();
                    writer.newLine();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            writer.flush();


        } catch (IOException e) {
            e.printStackTrace();
        }




    }
}